<?php
/**
 * Equip class, wear and remove equipment
 * @package action
 * @author alcaitiff
 */
class Equip implements Action{

    /**
     * @static
     * @var integer
     **/
	static private $tick;
    /**
     * @static
     * @var Char
     **/
	static private $char;
    /**
    * Usage: equip Item [number] [container [number]] | remove Item [number]
	*
    * The container may be on hands or floor, the item must be equipable
	* @static
    * @param Command $command Command
    * @param int $tick Tick
    * @return String This command will return a msg
    */
	static public function run($command,$tick){
		self::$tick=$tick;
		self::$char=$command->character;
        $line=$command->command;
        if($line=='equip'){
            $parameters=$command->getArrangedParameters(1,4,false);
            return self::equipItem($parameters[1], $parameters[2], (isset($parameters[3]))?$parameters[3]:null, (isset($parameters[4]))?$parameters[4]:null);
        }else{
            $parameters=$command->getArrangedParameters(1,2,false);
            return self::removeItem($parameters[1], $parameters[2]);
        }
	}
    /**
     * Equip the item
     * @param string $name
     * @param int $index
     * @param string $containerName
     * @param int $containerIndex
     * @return string
     */
    static private function equipItem($name,$index,$containerName,$containerIndex){
        $item=null;
        $container=null;

        //Get from floor
        if($containerName===null){
            $item = self::$char->getItem($name, $index);
        }else{
            //get container
            $container=self::$char->getItem($containerName, $containerIndex);
            if($container!==NULL){
                if($container->container!=null){
                    //get item from container
                    $item=$container->container->getItemByName($name,$index);
                }
            }
        }
        if($item===null){
            return null;
        }
        
        return self::$char->equip($item,$container);
    }

    static private function removeItem($name,$index){
        $item=self::$char->getEquipedItemByName($name,$index);
        if($item!=null){
            return self::$char->remove($item);
        }

    }
}